library RepairOutCommand initializer init requires LegacySystem

	globals
		private boolean isRepairing = false
 	endglobals
	
	private function RepairBay takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local real managain
		local real maxmana = GetUnitState(u,UNIT_STATE_MAX_MANA)
		local integer id = GetUnitTypeId(u)
		if IsUnitType(u,UNIT_TYPE_MECHANICAL)==true and id!='e000' and id!='e00N' and isRepairing==false then
			set isRepairing = true
			call PauseUnit(u,true)
			call DisplayTimedTextToPlayer( GetOwningPlayer(u),0.0,0.0,5.0, GetUnitName(u) + " is being repaired. Type -out to exit." )
			call SetUnitFlyHeight(u,10.0,20.0)
			call SetUnitColor( gg_unit_h00I_0094, PLAYER_COLOR_GREEN )
			call UnitAddAbility(u,'A056')
			call UnitAddAbility(u,'A057')
			call UnitAddAbility(u,'A03M')
			call UnitAddAbility(u,'Alrm')
			loop
				exitwhen (GetWidgetLife(u)==GetUnitState(u,UNIT_STATE_MAX_LIFE) and GetUnitState(u,UNIT_STATE_MANA)==maxmana) or isRepairing==false or GetWidgetLife(u)<0.405
				call DestroyEffect( AddSpecialEffectTarget( "Abilities\\Weapons\\ProcMissile\\ProcMissile.mdl",u,"chest" ))
				set managain = GetUnitState(u,UNIT_STATE_MANA)+5.0
				if managain>maxmana then
					set managain=maxmana
				endif
				call SetUnitState(u,UNIT_STATE_MANA,managain)
				call TriggerSleepAction(1.)
			endloop
			call SetUnitColor( gg_unit_h00I_0094, PLAYER_COLOR_LIGHT_GRAY )
			call UnitRemoveAbility(u,'A056')
			call UnitRemoveAbility(u,'A057')
			call UnitRemoveAbility(u,'A03M')
			call UnitRemoveAbility(u,'Alrm')
			call PauseUnit(u,false)
			call SetUnitFlyHeight(u,GetUnitDefaultFlyHeight(u),100.0)
			set isRepairing = false
		endif
		set u = null
	endfunction

	//===========================================================================
	function InitTrig_Repair_Bay takes nothing returns nothing
		
	endfunction



	private function RepairOutCommand takes nothing returns nothing
		set isRepairing = false
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		local integer i = 0
		set t = CreateTrigger()

		loop
			exitwhen i > 11
			call TriggerRegisterPlayerChatEvent(t, Player(i), "-out", true)
			set i = i + 1
		endloop

		call TriggerAddAction(t,function RepairOutCommand)
		
		set t = CreateTrigger()
		call TriggerRegisterEnterRectSimple(t,gg_rct_RepairBay)
		call TriggerAddAction(t,function RepairBay)
	endfunction
endlibrary